﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GraphTest.Geometry2D.Interfaces;

namespace GraphTest.Geometry2D
{
    public class Vector2D : IPoint2D
    {
        public Vector2D()
        {
            X = 0.0F;
            Y = 0.0F;
        }

        public Vector2D(float x, float y)
        {
            X = x;
            Y = y;
        }

        public Vector2D(IPoint2D p1, IPoint2D p2)
            : this(p1.X, p1.Y, p2.X, p2.Y)
        { }
        
        public Vector2D(float x1, float y1, float x2, float y2)
        {
            X = x2 - x1;
            Y = y2 - y1;
        }

        public Vector2D(IPoint2D p)
        {
            if (p == null)
                throw new ArgumentNullException("p");

            X = p.X;
            Y = p.Y;
        }


        public float X { get; set; }
        public float Y { get; set; }


        public Point2D GetPoint()
        {
            return new Point2D(X, Y);
        }

        public float Module()
        {
            return (float)Math.Sqrt(X * X + Y * Y);
        }

        public float ScalarMult(Vector2D v)
        {
            if (v == null)
                throw new ArgumentNullException("v");

            float result = X * v.X + Y * v.Y;
            return result;
        }

        public static float ScalarMult(Vector2D v1, Vector2D v2)
        {
            if (v1 == null)
                throw new ArgumentNullException("v1");
            if (v2 == null)
                throw new ArgumentNullException("v2");

            return v1.ScalarMult(v2);
        }

        public void Add(Vector2D v)
        {
            if (v == null)
                throw new ArgumentNullException("v");

            X += v.X;
            Y += v.Y;
        }

        public void Subst(Vector2D v)
        {
            if (v == null)
                throw new ArgumentNullException("v");

            X -= v.X;
            Y -= v.Y;
        }

        public void Mult(float c)
        {
            X *= c;
            Y *= c;
        }

        public Vector2D Normal()
        {
            return new Vector2D(-Y, X);
        }

        public static float VectorMultApplicate(Vector2D v1, Vector2D v2)
        {
            return v1.X * v2.Y - v1.Y * v2.X;
        }

        public static Vector2D Ux
        {
            get { return new Vector2D(1.0F, 0.0F); }
        }

        public static Vector2D Uy
        {
            get { return new Vector2D(0.0F, 1.0F); }
        }
    }
}
